package ph.com.gs3.formalistics.service.services;

import java.util.List;

import ph.com.gs3.formalistics.global.utilities.services.ServiceEventNotifierUtility;
import ph.com.gs3.formalistics.model.communicators.DocumentCommunicatorModule;
import ph.com.gs3.formalistics.model.communicators.HttpCommunicator;
import ph.com.gs3.formalistics.model.dao.DocumentsDAO;
import ph.com.gs3.formalistics.model.dao.OutgoingStarMarksDAO;
import ph.com.gs3.formalistics.model.valueobjects.business.User;
import ph.com.gs3.formalistics.model.valueobjects.business.document.OutgoingStarMark;
import ph.com.gs3.formalistics.presenter.MainActivity;
import ph.com.gs3.formalistics.service.managers.DocumentManager;
import ph.com.gs3.formalistics.service.managers.DocumentManager.DocumentActionSubmitFailedException;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;

public class OutgoingStarMarksSubmitService extends IntentService {

	// constants
	public static final String TAG = OutgoingStarMarksSubmitService.class.getSimpleName();
	public static final String NAME = OutgoingStarMarksSubmitService.class.getName();

	public static final String ACTION_ON_SERVICE_STARTED = NAME + "_on_service_started";
	public static final String ACTION_ON_SERVICE_DONE = NAME + "_on_service_done";

	public static final String EXTRA_CURRENT_USER = "current_user";

	// dependencies
	private DocumentsDAO documentsDAO;
	private OutgoingStarMarksDAO outgoingStarMarksDAO;

	private DocumentCommunicatorModule documentCommunicatorModule;
	private DocumentManager documentManager;

	// fields
	private User currentUser;

	public OutgoingStarMarksSubmitService() {
		super(NAME);
	}

	@Override
	protected void onHandleIntent(Intent intent) {

		initializeFieldsFromExtras(intent);
		initializeDependencies();

		// notify
		Bundle notificationExtras = new Bundle();
		notificationExtras.putSerializable(MainActivity.EXTRA_LOGGED_IN_USER, currentUser);

		ServiceEventNotifierUtility.getDefaultInstance().displayNotificationWithProgress(this,
		        ServiceEventNotifierUtility.NOTIFICATION_ID_PRIMARY,
		        "Sending your outgoing star marks", 0, 0, true, notificationExtras);

		broadcastServiceStarted();

		String userId = Integer.toString(currentUser.getId());

		// get all the outgoing star marks

		outgoingStarMarksDAO.open();
		List<OutgoingStarMark> starMarks = outgoingStarMarksDAO.getUserOutgoingStarMarks(userId);
		outgoingStarMarksDAO.close();

		for (OutgoingStarMark outgoingStarMark : starMarks) {

			try {
				documentManager.markDocumentStar(outgoingStarMark);
			} catch (DocumentActionSubmitFailedException e) {
				e.printStackTrace();
			}

		}

		broadcastServiceDone();

		ServiceEventNotifierUtility.getDefaultInstance().dismissPrimaryNotification(this);

	}

	private void initializeFieldsFromExtras(Intent intent) {

		currentUser = (User) intent.getExtras().getSerializable(EXTRA_CURRENT_USER);

	}

	private void initializeDependencies() {

		String server = currentUser.getAccountServer();

		HttpCommunicator httpCommunicator = new HttpCommunicator();
		documentCommunicatorModule = new DocumentCommunicatorModule(httpCommunicator, server);

		documentsDAO = new DocumentsDAO(getApplicationContext());
		outgoingStarMarksDAO = new OutgoingStarMarksDAO(getApplicationContext());

		/**
		 * This document manager does not have a forms dao or an outgoing documents dao.
		 * This will not be able to do transactions related to those two dependencies.
		 */
		documentManager = new DocumentManager(null, documentsDAO, null, outgoingStarMarksDAO,
		        documentCommunicatorModule);

	}

	// ========================================================================================
	// {{ Broadcast Methods

	private void broadcastServiceStarted() {

		Intent intent = new Intent();
		intent.setAction(ACTION_ON_SERVICE_STARTED);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		sendBroadcast(intent);

	}

	private void broadcastServiceDone() {

		Intent intent = new Intent();
		intent.setAction(ACTION_ON_SERVICE_DONE);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		sendBroadcast(intent);

	}

	// }}

}
